<?php
/**
 * Created by PhpStorm.
 * User: 王飞
 * Date: 2017/8/20
 * Time: 18:44
 */
namespace app\admin\controller\staff;

use app\admin\model\Order as OrderModel;
use app\common\controller\Backend;
use app\common\model\Category as CategoryModel;
use think\Session;

/**
 * Class Order
 * @package app\admin\controller\staff
 * 此类为订单类，管理员可以通过此类查看订单，并为员工添加工作，管理员在此处无法对订单进行操作
 */
class Order extends Backend
{
	protected $model = null;
	protected $serverdayslist=null;
	public function _initialize()
	{
		parent::_initialize();
		$this->model = model('Order');
	}
	
	/**
	 * 此函数为显示为分配的预约订单，可为员工添加面试
	 */
	public function index()
	{
		/**
		 * 进入员工管理页面中，只有查看功能和添加面试功能，没有修改功能
		 */
		if ($this->request->isAjax()) {
			list($where, $sort, $order, $offset, $limit) = $this->buildparams(NULL);
			$total = $this->model->alias('a')
				->where($where)->where('staff_id',$_GET['userid'])
				->order($sort, $order)
				->count();
			$list = $this->model->alias('a')
				->where($where)->where('staff_id',$_GET['userid'])
				->order($sort, $order)
				->limit($offset, $limit)
				->select();
			$result = array("total" => $total, "rows" => $list);
			
			return json($result);
		}
		$this->view->assign('staffid',input('get.userid'));
		$this->view->assign('type',input('get.type'));
		return $this->view->fetch();
	}
	
	/**
	 * 增加面试，此环节为增加面试，仅通过面才可以为员工添加工作
	 */
	public function  addinterview(){
		/***
		 * step1:c初始化控件，添加面试时间，仅添加面试时间，其他信息为自动生成
		 */
		$staffid=$_GET['staffid'];
		$orderid=$_GET['orderid'];
        if ($this->request->isAjax()) {
	        $this->code = -1;
            $data['begintime']=$_POST['row']['begintime'];
	        $data['endtime']=date("Y-m-d",strtotime("+1 day",strtotime($_POST['row']['begintime'])));
            $data['order_id']=$_POST['row']['order_id'];
            $data['staff_id']=$_POST['row']['staff_id'];
            $data['admin_id']=Session::get('admin')->id;
            $data['createtime']=date('Y-m-d',time());
            $data['w_type']=3;
	        $data['facetype']=1;
	        
	        $isfree=db('worklog')
		        ->where('begintime','<= time',$data['begintime'])
		        ->where('endtime','> time',$data['begintime'])
		        ->whereor('begintime',['>=',$data['begintime']],['<',$data['endtime']],'and')
		        ->whereor('endtime',['>',$data['begintime']],['<',$data['endtime']],'and')
		    ->find();
	        if($isfree!=null){
		        $this->code = -1;
		        $this->msg="这一天已有安排，请查看！";
		        return;
	        }
	        
	        $user = model('Worklog');
	        $order = OrderModel::get(intval($_POST['row']['order_id']));
	        $order->status     = 3;
	        $result=$order->save();
	        if ($result !== false)
	        {
		        $this->code = 1;
	        }
	        else
	        {
		        $this->msg = $this->model->getError();
		        return;
	        }
	       
	        db('worklog')->data($data)->insert();
	        $data['w_type']=4;
	        $result=db('worklog')->data($data)->insert();
	        if ($result !== false)
	        {
		        $this->code = 1;
	        }
	        else
	        {
		        $this->msg = $this->model->getError();
	        }
            return;
        }
        $this->view->assign('staff_id',$_GET['staffid']);
        $this->view->assign('order_id',$_GET['orderid']);
		return $this->view->fetch();

	}
	
}